package com.monster.dict.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.monster.dict.domain.common.DictValidGroup;
import com.monster.dict.domain.dto.DictInsertDto;
import com.monster.dict.domain.dto.DictUpdateDto;
import com.monster.dict.domain.dto.QueryDictListDto;
import com.monster.dict.domain.service.SysDictService;
import com.monster.dict.domain.vo.QueryDictListVo;
import com.monster.starter.core.common.entity.RequestData;
import com.monster.valid.annotation.ValidMonster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * pc 字典类型 视图层
 *
 * @author kuang
 */
@Validated
@RestController
@RequestMapping("dict")
public class SysDictController {
    /**
     * 字典类型的service层
     */
    @Autowired
    private SysDictService dictService;

    /**
     * 通过字典编码查询字典类型
     *
     * @param requestData 分页查询条件
     * @return List<DictTreeVo>
     */
    @PostMapping("list")
    public IPage<QueryDictListVo> list(@RequestBody @Validated(DictValidGroup.Query.class) RequestData<QueryDictListDto> requestData) {
        return dictService.list(requestData.getPage(), requestData.getData());
    }

    /**
     * 新增字典数据
     *
     * @param insertDto 新增数据实体
     * @return boolean
     */
    @PostMapping("insert")
    public boolean insert(@RequestBody @Validated(DictValidGroup.Insert.class) DictInsertDto insertDto) {
        return dictService.insert(insertDto);
    }

    /**
     * 修改数据字典类型
     *
     * @param updateDto 修改数据
     * @return boolean
     */
    @PostMapping("update")
    public boolean update(@RequestBody @Validated(DictValidGroup.Update.class) DictUpdateDto updateDto) {
        return dictService.update(updateDto);
    }

    /**
     * 通过id删除数据
     *
     * @param id 数据id
     * @return boolean
     */
    @GetMapping("del/{id}")
    public boolean delete(@PathVariable @ValidMonster(name = "数据id", isNull = false, max = 25, min = 15) String id) {
        return dictService.delete(id);
    }
}
